// 节流
function throttled(fn, delay = 1000) {
	let startTime = 0

	return function() {
		let curTime = Date.now()
		let remaining = delay - (curTime - startTime)

		if (remaining <= 0) {
			fn.apply(this, arguments)
			startTime = curTime
		}
	}
}

function throttled2(fn, delay = 1000) {
	let timer = null
	return function(...args) {
		if (!timer) {
			fn.apply(this, args)
			timer = setTimeout(() => {
				timer = null
			}, delay);
		}
	}
}

var n = 0

var thr = throttled(function() {
	console.log("321-" + n)
})

// while (n < 100) {
// 	n++
// 	thr()
// 	console.log("123-" + n)
// }

var inv = setInterval(function() {
	n++
	thr()
	console.log("123-" + n)
	if (n >= 10) clearInterval(inv)
}, 500)